package junior.db.dao.base;

import com.google.common.collect.Maps;
import junior.db.dao.enums.LikeMode;
import junior.db.dao.enums.Operator;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author wubo
 * @date 2024/8/9 16:26
 */
public class QueryLink<Q, J extends AbstractFilter> implements IFilter<QueryLink<Q, J>> {
    protected Map<String, String> joinFields;
    private Q mainQuery;
    private J joinQuery;
    
    QueryLink(Q mainQuery, J joinQuery) {
        this.mainQuery = mainQuery;
        this.joinQuery = joinQuery;
    }
    
    /**
     * 关联表间过滤字段，EQ过滤
     * @param mainField 主实体字段
     * @param joinField 从实体字段
     * @return
     */
    public QueryLink<Q, J> on(String mainField, String joinField) {
        return on(mainField, Operator.EQ, joinField);
    }
    /**
     * 关联表间过滤字段，根据指定操作符过滤
     * @param mainField 主实体字段
     * @param operator 过滤操作符
     * @param joinField 从实体字段
     * @return
     */
    public final QueryLink<Q, J> on(String mainField, Operator operator, String joinField) {
        if (joinFields == null) {
            joinFields = Maps.newHashMap();
        }
        joinFields.put(mainField, joinField);
        
        joinQuery.addFilter(new Filter(mainField, operator, joinField).withRightAsField());
        return this;
    }
    
    public Q get() {
        return mainQuery;
    }
    
    public J getJoinQuery() {
        return joinQuery;
    }
    
    @Override
    public List<IFilterItem> getFilters() {
        return joinQuery.getFilters();
    }
    
    @Override
    public QueryLink<Q, J> addFilter(IFilterItem filter) {
        joinQuery.addFilter(filter);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> eq(String left, Object value) {
        joinQuery.eq(left, value);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> neq(String left, Object value) {
        joinQuery.neq(left, value);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> gt(String left, Object value) {
        joinQuery.gt(left, value);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> lt(String left, Object value) {
        joinQuery.lt(left, value);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> gte(String left, Object value) {
        joinQuery.gte(left, value);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> lte(String left, Object value) {
        joinQuery.lte(left, value);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> nvl(String left) {
        joinQuery.nvl(left);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> notNvl(String left) {
        joinQuery.notNvl(left);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> in(String left, Collection<?> values) {
        joinQuery.in(left, values);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> in(String left, Object[] values) {
        joinQuery.in(left, values);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> notIn(String left, Collection<?> values) {
        joinQuery.notIn(left, values);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> notIn(String left, Object[] values) {
        joinQuery.notIn(left, values);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> like(String left, Object value, LikeMode likeMode) {
        joinQuery.like(left, value, likeMode);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> notLike(String left, Object value, LikeMode likeMode) {
        joinQuery.notLike(left, value, likeMode);
        return this;
    }
    
    @Override
    public QueryLink<Q, J> bt(String left, Object value1, Object value2) {
        joinQuery.bt(left, value1, value2);
        return this;
    }
}
